package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Employee;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 *
 */
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Transactional
    @Override
    public void saveWithFlavors(DishDTO dishDTO) {
        /**
         * 保存菜品数据
         * 1.创建Dish对象
         * 2.将dishDTO拷贝到dish对象中
         * 3.将dish插入到表中（注意：需要插入以后的返回值id）
         * 4.从dishDTO获取到dishFlavors集合
         * 5.遍历集合，设置dishId
         * 6.批量插到dish_flavor表中
         */
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);

        dishDTO.getFlavors().forEach(dishFlavor -> {
            //设置dishId
            dishFlavor.setDishId(dish.getId());
        });
        dishFlavorMapper.insertBatch(dishDTO.getFlavors());
    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());

        Page page = dishMapper.page(dishPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    @Transactional
    @Override
    public void delete(Long[] ids) {
        //删除菜品
        //--1.如果是启售中不能删除
        //--2.如果套餐中有关联则不能删除
        //--3.如果有口味，则顺带把口味删除
        for (Long id : ids){
            Dish dish = dishMapper.findById(id);
            if (dish.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }

            List<SetmealDish> setmealDishes = setmealDishMapper.getByDishId(id);
            if (setmealDishes != null && setmealDishes.size()>0){
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
            dishMapper.delete(id);
            dishFlavorMapper.deleteByDishId(id);
        }
    }

    @Override
    public DishVO getByIdWithFlavor(Long id) {
        Dish dish = dishMapper.findById(id);
        List<DishFlavor> dishflavors =dishFlavorMapper.findByDishId(id);
        DishVO dishVO = new DishVO();
        dishVO.setFlavors(dishflavors);
        BeanUtils.copyProperties(dish,dishVO);
        return dishVO;
    }

    @Transactional
    @Override
    public void updateWithFlavors(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //1.修改菜品表
        dishMapper.update(dish);
        //2.删除菜品-口味表中对应的数据
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        //3.重新插入菜品-口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();

        flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishDTO.getId()));

            dishFlavorMapper.insertBatch(flavors);
        }


    @Override
    public void startOrStop(Integer status, Long id) {
        Dish dish=new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.update(dish);
    }
    @Override
    public List<DishVO> list(Long categoryId) {
        return dishMapper.list(categoryId);
    }

    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<DishVO> dishList = dishMapper.list(dish.getCategoryId());
        for (DishVO d : dishList) {
            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.findByDishId(d.getId());
            d.setFlavors(flavors);
        }

        return dishList;
    }
}